package org.deadline.model.baseapp;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class Address extends BaseEntity  implements Serializable {

    @NotNull
    @Size(min = 3, max = 64)
    private String addressLine1;

    @NotNull
    @Size(min = 3, max = 64)
    private String addressLine2;
    
    @NotNull
    @Size(min = 3, max = 100)
    private String city;

    @Size(max = 32)
    private String stateCode;

    
    @Size(max = 32)
    private String territoryCode;

    @NotNull
    @Size(min = 3, max = 64)
    private String country;

    @NotNull
    @Size(min = 3, max = 3)
    private String countryCode;

    @NotNull
    @Size(min = 3, max = 20)
    private String postalCode;

    @ManyToOne
    private Principal principal;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "address")
    private Set<CommunicationChannel> communicationChannels = new HashSet<CommunicationChannel>();

	@Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="address_seq_gen",sequenceName="address_seq")
	@GeneratedValue(generator="address_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

	
	private static final long serialVersionUID = 1L;

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static Address fromJsonToAddress(String json) {
        return new JSONDeserializer<Address>().use(null, Address.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Address> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Address> fromJsonArrayToAddresses(String json) {
        return new JSONDeserializer<List<Address>>().use(null, ArrayList.class).use("values", Address.class).deserialize(json);
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String getAddressLine1() {
        return this.addressLine1;
    }

	public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

	public String getAddressLine2() {
        return this.addressLine2;
    }

	public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

	
	public String getCity() {
        return this.city;
    }

	public void setCity(String city) {
        this.city = city;
    }

	public String getStateCode() {
        return this.stateCode;
    }

	public void setStateCode(String stateCode) {
        this.stateCode = stateCode;
    }

	public String getTerritoryCode() {
        return this.territoryCode;
    }

	public void setTerritoryCode(String territoryCode) {
        this.territoryCode = territoryCode;
    }

	public String getCountry() {
        return this.country;
    }

	public void setCountry(String country) {
        this.country = country;
    }

	public String getCountryCode() {
        return this.countryCode;
    }

	public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

	public String getPostalCode() {
        return this.postalCode;
    }

	public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

	public Principal getPrincipal() {
        return this.principal;
    }

	public void setPrincipal(Principal principal) {
        this.principal = principal;
    }

	public Set<CommunicationChannel> getCommunicationChannels() {
        return this.communicationChannels;
    }

	public void setCommunicationChannels(Set<CommunicationChannel> communicationChannels) {
        this.communicationChannels = communicationChannels;
    }

	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	}
